home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Nebula 1
/
Nebula One.iso
/
Mail
/
pine3.92
/
pine
/
osdep
/
readfile
< prev
next >
Wrap
Text File
|
1995-11-22
|
944b
|
43 lines
/*----------------------------------------------------------------------
Read whole file into memory
Args: filename -- path name of file to read
Result: Returns pointer to malloced memory with the contents of the file
or NULL
This won't work very well if the file has NULLs in it and is mostly
intended for fairly small text files.
----*/
char *
read_file(filename)
char *filename;
{
int fd;
struct stat statbuf;
char *buf;
int nb;
fd = open(filename, O_RDONLY);
if(fd < 0)
return((char *)NULL);
fstat(fd, &statbuf);
buf = fs_get((size_t)statbuf.st_size + 1);
/*
* On some systems might have to loop here, if one read isn't guaranteed
* to get the whole thing.
*/
if((nb = read(fd, buf, (int)statbuf.st_size)) < 0)
fs_give((void **)&buf); /* NULL's buf */
else
buf[nb] = '\0';
close(fd);
return(buf);
}